package ch.android.reasoning;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Random;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;


public class AndroidReasoning extends Activity {
	
	private static HiddenMarkovModel rainmanHmm;
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        TextView tv = new TextView(this);
        String textOut = "";
        RandomVariable rv = new RandomVariable();
        Random randomGenerator = new Random();
		rainmanHmm = constructHmm();
		List<String> perceptions = new ArrayList<String>();
		
		textOut = textOut+("Creating 1000 percept Hidden Markov Model \n");

	    for (int idx = 1; idx <= 1000; ++idx)
	    {
	      int randomInt = randomGenerator.nextInt(100);
	      
	      if(randomInt%2 == 0) perceptions.add(HmmConstants.SEE_UMBRELLA);
	      if(randomInt%2 != 0) perceptions.add(HmmConstants.SEE_NO_UMBRELLA);
	    }
	    long start = System.currentTimeMillis();
	    List<RandomVariable> results = rainmanHmm.forward_backward(perceptions);
	    long stop = System.currentTimeMillis(); 
	    rv = results.get(results.size()-1);
		textOut = textOut+" P(RAINING| e_n)="+
				(Double.toString(rv.getProbabilityOf(HmmConstants.RAINING))).substring(0, 6)+"	P(NOT_RAINING| e_n)="+
				(Double.toString(rv.getProbabilityOf(HmmConstants.NOT_RAINING))).substring(0, 6)+"\n";
		textOut = textOut+" Execution time in millis:"+(stop-start);
        tv.setText(textOut);
        setContentView(tv);
    }
    
    private HiddenMarkovModel constructHmm()
    {
		List<String> states = Arrays.asList(new String[] {
				HmmConstants.RAINING, HmmConstants.NOT_RAINING });
		RandomVariable prior = new RandomVariable(states);
		
		TransitionModel tm = new TransitionModel(states);
		// tm.setTransitionModelValue(start_state, action, end_state,
		// probability);
		// given a start state and an action the probability of the end state is
		// probability
		tm.setTransitionProbability(HmmConstants.RAINING, HmmConstants.RAINING, 0.7);
		tm.setTransitionProbability(HmmConstants.RAINING, HmmConstants.NOT_RAINING, 0.3);
		tm.setTransitionProbability(HmmConstants.NOT_RAINING, HmmConstants.RAINING, 0.3);
		tm.setTransitionProbability(HmmConstants.NOT_RAINING, HmmConstants.NOT_RAINING, 0.7);
		
		// no actions because the observer has no way of changing the hidden
		// state and is passive
		List<String> perceptions = Arrays.asList(new String[] {
				HmmConstants.SEE_UMBRELLA, HmmConstants.SEE_NO_UMBRELLA });

		SensorModel sm = new SensorModel(states, perceptions);
		// sm.setSensingProbaility(state,perception,p); given a state the
		// probability of a perception is p
		sm.setSensingProbability(HmmConstants.RAINING, HmmConstants.SEE_UMBRELLA, 0.9);
		sm.setSensingProbability(HmmConstants.RAINING, HmmConstants.SEE_NO_UMBRELLA, 0.1);
		sm.setSensingProbability(HmmConstants.NOT_RAINING, HmmConstants.SEE_UMBRELLA, 0.2);
		sm.setSensingProbability(HmmConstants.NOT_RAINING, HmmConstants.SEE_NO_UMBRELLA, 0.8);

		HiddenMarkovModel hmm = new HiddenMarkovModel(prior, tm, sm);

		// hmm.setSensorModelValue(state,perception,p); given a state the
		// probability of a perception is p

		return hmm;
    }
}